package com.qf.hotel.controller.foodtype;

import com.qf.hotel.controller.BaseServlet02;
import com.qf.hotel.factory.BeanFactory;
import com.qf.hotel.pojo.FoodType;
import com.qf.hotel.service.FoodTypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/foodtype04")
public class FoodTypeController04 extends BaseServlet02 {
    public void findById(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("foodtype04调用findById方法...");
    }

    public void update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("foodtype04调用update方法...");
    }

    public void search(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        System.out.println("foodtype04调用search方法...");
        try {
            // 2、接收参数
            String keyword = request.getParameter("keyword");

            // 3、创建FoodType对象，设置typeName
            FoodType foodType = new FoodType();
            foodType.setTypeName(keyword);

            // 4、创建业务对象
            // FoodTypeService foodTypeService = new FoodTypeServiceImpl();
            FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean("foodTypeService");
            List<FoodType> foodTypes = foodTypeService.findByCondition(foodType);

            // 5、存储在域对象中
            request.setAttribute("foodTypes",foodTypes);

            // 6、转发到显示数据的页面
            request.getRequestDispatcher("/backend/detail/foodtype/foodtype-list.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<String> findByCondition(HttpServletRequest request, HttpServletResponse response) {
        ArrayList list = new ArrayList();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        System.out.println("foodtype04调用findByCondition方法...");
        return list;
    }
}
